ENTITY

ENTITY attributes are pointers to external entities.  So far we have been working with standalone documents.  Standalone documents have no references to external entities (they are complete and self-contained).  The value of an ENTITY attribute must consist of characters that represent an external entity (storage object).  The syntax for declaring an ENTITY attribute is:

<!ATTLIST element.name ENTITY>

We are going to go into detail on referencing external entities later.  

ENTITIES

ENTITIES attributes are the same as ENTITY attributes, except they reference multiple external entities.  Like IDREFS, the values must be separated by a space.    The syntax for declaring an ENTITIES attribute is:

<!ATTLIST element.name ENTITIES>

NMTOKEN

NMTOKEN attributes have values that are token strings.  These token strings can consist of any mixture of name characters.  The syntax for NMTOKEN attributes is:

<!ATTLIST element.name NMTOKEN>

NMTOKENS

NMTOKEN attributes are the same as NMTOKEN attributes, except that they can contain multiple values provided that they are separated by spaces.  Tokens are similar to string typess, but the are processed differently.  NMTOKENS attribute values are treated each independently by an XML processor and can be queried independently be an application.

<?xml version="1.0"?>
<!DOCTYPE tokens [

	<!ELEMENT tokens ( element1+)>

	<!ELEMENT element1 ( #PCDATA )>
		<!ATTLIST element1 attr1 NMTOKEN #IMPLIED>
		<!ATTLIST element1 attr2 NMTOKENS #IMPLIED>
]>
<tokens>

	<element1 attr1="Token1" 
			 attr2="123_Token1 Token2 Token3">
		Some text here...
	</element1>

</tokens>

File: Chapter 4\TokenAttributes.xml

Enumerated Type

Enumerated attributes have values that are part of a list of possible values.  These values must be valid name tokens. The syntax is:

<!ATTLIST element.name attribute.name 
		( option1 | option 2 | option n ) 
		“default value”>

Let’s say that you want to declare an element called <primary> with an attribute called “color”.  The only valid primary color values are “RED”, “GREEN” and “BLUE”, and we want to force our DTD to constrain our possible values to these.  We also want the default value to be “RED”.  

Here is the resulting XML document.

<?xml version="1.0"?>
<!DOCTYPE colors [

	<!ELEMENT colors ( primary+ )>

	<!ELEMENT primary EMPTY>
		<!ATTLIST primary color ( RED | GREEN | BLUE ) "RED">
]>
<colors>

	<primary color="RED"/>
	<primary color="GREEN"/>
	<primary color="BLUE"/>

	<!-- ERROR!
	<primary color="YELLOW"/>
	-->

</colors>

File: Chapter 4\EnumerationAttributes.xml

There is no such thing as an optional attribute in XML.  However, using enumeration attributes, you can emulate an optional value.  This is accomplished by defining the default value in the <!ATTLIST> declaration as an empty string.  

<!ATTLIST elem attr (X | Y | Z) “”>

If you leave the attribute out, the default value, which is an empty string, will be assigned to the attribute.  In other words, you do not have to assign it in the element.